<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service;

use App\Constants\ErrorCode;
use App\Dao\UserDiaryDao;
use Hyperf\Di\Annotation\Inject;

class UserDiaryService
{
    /**
     * @Inject
     * @var UserDiaryDao
     */
    protected $userDiaryDao;

    /**
     * 增加一个日记.
     * @param $userId
     * @param $data
     */
    public function addDiary($userId, $data): bool
    {
        $data['user_id'] = $userId;
        $this->userDiaryDao->addDiary($data);
        return true;
    }

    /**
     * 删除日记.
     * @param $userId
     * @param $id
     */
    public function deleteDiary($userId, $id): bool
    {
        $info = $this->userDiaryDao->getDiaryInfo($userId, $id);
        if (empty($id) || empty($info)) {
            throw new \Exception('日记不存在', ErrorCode::PARAM_ERROR);
        }
        $this->userDiaryDao->deleteDiary($id);
        return true;
    }

    /**
     * 获取日记详情.
     * @param $userId
     * @param $id
     */
    public function getDiaryInfo($userId, $id): array
    {
        return $this->userDiaryDao->getDiaryInfo($userId, $id);
    }
}
